<?php


/**
 * 最离现在时间最早的提交的待确认的订单
 * @return array
 */
function get_un_search_order_goods(){
    global $db;

    $sql = <<<EOD

    SELECT
       distinct(concat(og.goods_id,',',o.keywords)) AS item_keywords
       FROM orders o
       INNER JOIN order_goods og ON og.order_id = o.order_id
       WHERE search_date = '0000-00-00 00:00:00'
       ORDER BY og.id ASC
EOD;

    //读取需要抓取的列表
    $goods = $db->get_all($sql);

    if(!empty($goods)){

        $goods_fmt = array();
        foreach($goods as $g){
            list($item_id,$keywords) = explode(',',$g['item_keywords']);

            if($item_id > 0){
                $goods_fmt[] = array(
                    'item_id'   => $item_id,
                    'keywords'  => $keywords
                );
            }
        }
        return $goods_fmt;
    }else{
        return array();
    }
}



/**
 * 修改订单的内容
 * @param $order_id
 * @param $up_items
 * @return int
 */
function goods_update_search_rank($item_id,$keywords,$rank){
    global $db;

    $search_date = full_date();

    $sql = <<<EOD
    UPDATE order_goods og
    INNER JOIN orders o ON o.order_id = og.order_id
    SET og.search_rank = $rank,
        og.search_date = '$search_date'
    WHERE og.search_date = '0000-00-00 00:00:00'
      AND og.goods_id = $item_id AND o.keywords = '$keywords'
EOD;

    $db->query($sql);
    return $db->affected_rows();

}